package com.example.web.controller;

import com.example.web.mapper.UserMapper;
import com.example.web.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * Create by keboom on 2020/7/4
 */
@Controller
public class RegisterController {

    @Autowired
    private UserMapper userMapper;

    /**
     * 注册
     * @param username
     * @param password
     * @param birthday
     * @param gender
     * @param model
     * @return
     */
    @PostMapping("/mall/register")
    public String register(String username, String password,
                           String birthday,String gender,Model model){
        String forPassword = userMapper.selectByNameForPassword(username);
        if (forPassword == null) {
            User user = new User();
            user.setUserName(username);
            user.setUserPassword(password);
            user.setUserBalance(0);
            user.setUserBirthday(birthday);
            user.setUserSex(gender);
            userMapper.insertUser(user);
            return "redirect:login";
        }else {
            model.addAttribute("msg","用户名已经被注册~~~");
            return "register";
        }
    }
}
